//
//  KZBaseViewController.swift
//  Fanrengame
//
//  Created by 朱凯 on 2018/4/18.
//  Copyright © 2018年 JooKey. All rights reserved.
//

import UIKit

class KZBaseViewController: UIViewController {
    
    // MARK: 内嵌类型
    enum KZNavigationItemPosition {
        case left
        case right
    }
    enum KZNavigationItemMode {
        case text
        case image
        case mix
    }
    
    // MARK: 常量
    private let _itemBaseTag = 100;
    
    // MARK: 可修改属性
    lazy var tintColor: UIColor = UIColor.white
    
    override func viewDidLoad() {
        super.viewDidLoad()
        

        self.view.backgroundColor = UIColor.white
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // 设置导航栏和状态栏默认样式
        setNavigationBackgroungAttributes(naviTranslucent: false,
                                          naviBgColor: kThemeColor)
    }
    
    
    // MARK: 全局api
    // 设置导航栏和状态栏的一些状态
    // 导航栏的默认状态是不透明，白色背景；状态栏的默认状态是透明背景，黑色字体
    func setNavigationBackgroungAttributes(naviTranslucent translucent: Bool ,
                                           naviBgColor: UIColor = kThemeColor,
                                           statusBarStyle style: UIStatusBarStyle = .lightContent,
                                           statusBarHidden hidden: Bool = false) {
        
        UIApplication.shared.isStatusBarHidden = hidden
        UIApplication.shared.statusBarStyle = style
        
        self.navigationController?.navigationBar.isTranslucent = translucent
        if !translucent {
            self.navigationController?.navigationBar.setBackgroundImage(naviBgColor.image, for: .default)
//            self.navigationController?.navigationBar.shadowImage = UIColor.init(white: 0.8, alpha: 0.5).image
            self.navigationController?.navigationBar.shadowImage = UIColor.clear.image

        } else {
            self.navigationController?.navigationBar.setBackgroundImage(UIColor.clear.image, for: .default)
            self.navigationController?.navigationBar.shadowImage = UIColor.clear.image
        }
    }
    // 添加返回按钮
    func addBackItem() {
        
        // 创建按钮
        let backBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        backBtn.setImage(UIImage.init(named: "返回"), for: .normal)
        backBtn.imageEdgeInsets = UIEdgeInsets(top: 12, left: 0, bottom: 12, right: 20)
        backBtn.addTarget(self, action: #selector(leftAction(sender:)), for: .touchUpInside)
        
        // 这里按钮
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: backBtn)
    }
    // 添加导航栏元素：使用自带类型，item传nil默认调用创建返回item
    func addNavigationItems(items: [String]?,
                            position: KZNavigationItemPosition = .left,
                            mode: KZNavigationItemMode = .text) {
        
        // 如果item传了nil，则默认是创建返回item
        if let items = items {
            
            var views = [UIView]()
            var width: CGFloat = 0.0, height: CGFloat = 0.0
            for item in items {
                switch mode {
                case .text:
                    width = item.width(fontSize: 16) + 10.0
                    height = width
                    let button = UIButton(frame: CGRect(x: 0, y: 0, width: width, height: height))
                    button.setTitle(item, for: .normal)
                    button.setTitleColor(tintColor, for: .normal)
                    let action = position == KZNavigationItemPosition.left ? #selector(leftAction(sender:)) : #selector(rightAction(sender:))
                    button.addTarget(self, action: action, for: .touchUpInside)
                    button.titleLabel?.font = kFont(16)
                    button.titleEdgeInsets = position == KZNavigationItemPosition.left ? UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10) : UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
                    
                    views.append(button)
                    
                case .image:
                    width = 44
                    height = width
                    let button = UIButton(frame: CGRect(x: 0, y: 0, width: width, height: height))
                    button.setImage(UIImage.init(named: item), for: .normal)
                    let action = position == KZNavigationItemPosition.left ? #selector(leftAction(sender:)) : #selector(rightAction(sender:))
                    button.addTarget(self, action: action, for: .touchUpInside)
                    button.imageEdgeInsets = position == KZNavigationItemPosition.left ? UIEdgeInsets(top: 12, left: 0, bottom: 12, right: 20) : UIEdgeInsets(top: 12, left: 20, bottom: 12, right: 0)
                    
                    views.append(button)
                    
                    
                case .mix:
                    width = item.width(fontSize: 12)
                    width = width > 20 ? width : 20
                    height = 44
                    let margin: CGFloat = 4
                    
                    let view = UIControl(frame: CGRect(x: 0, y: 0, width: width, height: height))
                    let action = position == KZNavigationItemPosition.left ? #selector(leftAction(sender:)) : #selector(rightAction(sender:))
                    view.addTarget(self, action: action, for: .touchUpInside)
                    
                    let label = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: 12))
                    label.font = kFont(12)
                    label.text = item
                    label.textColor = tintColor
                    label.textAlignment = .center
                    view.addSubview(label)
                    
                    let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
                    imageView.image = UIImage(named: item)
                    view.addSubview(imageView)
                    
                    let remainY = view.height - imageView.height - label.height - margin
                    imageView.top = remainY / 2;
                    imageView.x = view.width / 2;
                    label.top = imageView.bottom + margin;
                    
                    views.append(view)
                    
                }
            }
            addNavigationItems(items: views, position: position)
        }
        else {
            addBackItem()
        }
    }
    // 添加导航栏元素：使用现成的view
    func addNavigationItems(items: [UIView]?, position: KZNavigationItemPosition) {
        
        var barItems = [UIBarButtonItem]()
        if let items = items {
            for view in items {
                view.tag = _itemBaseTag + items.index(of: view)!
                let barItem = UIBarButtonItem(customView: view)
                barItems.append(barItem)
            }
            if position == .left {
                self.navigationItem.leftBarButtonItems = barItems
            } else {
                self.navigationItem.rightBarButtonItems = barItems
            }
        } else {
            addBackItem()
        }
    }
    
    
    // MARK: 点击事件
    // 返回
    @objc private func leftAction(sender: UIControl) {
        leftAction(index: sender.tag - _itemBaseTag)
    }
    @objc private func rightAction(sender: UIControl) {
        rightAction(index: sender.tag - _itemBaseTag)
    }
    
    // MARK: 子类点击事件设置，需要覆写这两个方法
    func leftAction(index: Int) {
        self.navigationController?.popViewController(animated: true)
    }
    func rightAction(index: Int) {
        print("子类请覆写")
    }
}
